package 美团;

import 树.TreeNode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class _515_在每个树行中找最大值 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new LinkedList<>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            int maxValue = queue.peek().val;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                maxValue = Math.max(maxValue, node.val);
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            res.add(maxValue);
        }
        return res;
    }
}
